import { addDiary, addDiaryMessage, getDiaryPage, getMyDiaryPage, opraDiaryLikeStatus, 
  getDiaryMessageByDiaryId, addReplyDiaryMessage, getDiaryEmoji, getDiaryByRiZhu, getDiaryByDiaryId, 
  getLevel2MessgaeByMessageId, getTGZRPage, getDiaryByHourGanWithDay
 } from '@/api/diary'
import { avatarUrl } from '@/service'
import ava from '@/static/common/ava.png'


export class DiaryController {


  static  emojiMap = new Map([
    ['心情指数', 'mood'],
    ['钱财收支', 'money'],
    ['健康状况', 'health'],
    ['工作学习', 'work'],
    ['感情生活', 'relationship'],
  ])

  static async byId  (diaryId: string) {
    const { data } =  await getDiaryByDiaryId(diaryId)

    return {
      data: {
        ...data,
        user: {
          ...data.user,
          // avatar: avatarUrl + data.user['avatar']
          avatar: ava
        }
      }
    }
  }

  static async addDiary (params: API.DIARY) {
    const {status, msg} = await addDiary(params)
    status === 200 ? uni.showToast({title: '新增成功', icon: 'success' }) : uni.showToast({title: msg,icon: 'fail'})
    return status
  }

  static async addDiaryMessage (params: API.MESSAGE) {
    const {status, msg} = await addDiaryMessage(params)
    status === 200 ? uni.showToast({title: '回复成功', icon: 'success' }) : uni.showToast({title: msg,icon: 'fail'})
    return status
  }

  static async getDiaryPage (params: API.DIARYPAGE) {
    const { data } = await getDiaryPage(params)
   
    return {
      records: data.records.map(item => {
        return {  
          ...item, 
          user: {
            ...item.user, 
            // avatar: avatarUrl + item.user['avatar']
            avatar: ava
          }}
      }),
      total: data.total
    } 
  }

  static async getMyDiaryPage (page: number) {
    const {data} = await getMyDiaryPage(page)
    return data
  }

  static async getMessageByDiaryId (diaryId: string, page: number, pageSize: number = 10) {
    const { data } = await getDiaryMessageByDiaryId(diaryId, page, pageSize)
    return {
      data: data.records,
      total: data.total
    }
  }

  static async getLevel2MessgaeByMessageId (message: string, page: number) {
    const { data } = await getLevel2MessgaeByMessageId(message, page)
    return {
      data: data.records,
      total: data.total
    }
  }

  static async opraDiaryLikeStatus (diaryId: string, _status: 0 | 1) {
    const {status, msg} = await opraDiaryLikeStatus(diaryId, _status)
    if (status === 200) {
      console.log('_status');
      
      _status ? uni.showToast({title: '点赞成功', icon: 'success' }) : ''
    } else {
      uni.showToast({title: msg,icon: 'fail'})
    }
    return status
  }

  // 回复留言
  static async addReplyDiaryMessage (params: API.MESSAGE) {
    const {status, msg} = await addReplyDiaryMessage(params)
    status === status? uni.showToast({title: '回复成功', icon:'success' }) : uni.showToast({title: msg,icon: 'fail'}) 
    return status
  }

  // 获取表情包
  static async getDiaryEmoji () {
    const { data } = await getDiaryEmoji()
    return data.map(item => {
      return {
        ...item,
        keyword: DiaryController.emojiMap.get(item.title)
      }
    })
  }

  static async getDiaryByRiZhu (params: API.RIZHU) {
    const { data } = await getDiaryByRiZhu(params)
    return data
  }

  static async getTGZRPage (time: string, page: number) {
    const { data } = await getTGZRPage(time, page)
    return {
      data: data.records,
      total: data.total
    }
  }

  static async getDiaryByHourGanWithDay (params: {hourGan: string, day: string,  page: number}) {
    const { data } = await getDiaryByHourGanWithDay(params.hourGan, params.day, params.page)
    return {
      records: data.records,
      total: data.total
    }
  }
}